package com.luoyuanxiangvip.admin.core.task;

import com.luoyuanxiangvip.core.entity.BizLinks;
import com.luoyuanxiangvip.core.service.IBizLinksService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.net.ssl.HttpsURLConnection;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 定时任务
 * </p>
 *
 * @author luoyuanxiang <p>luoyuanxiangvip.com</p>
 * @since 2019/7/2
 */
@Slf4j
@Component
@EnableScheduling
public class TaskScheduled {

    @Resource
    private IBizLinksService iBizLinksService;

    /**
     * 定时任务，每月月初执行一次
     */
    @Async
    @Scheduled(cron="0 0 0 1 * ?")
    public void task() {
        log.info("开始检查友情链接");
        List<BizLinks> list = iBizLinksService.list();
        for (BizLinks bizLinks : list) {
            request(bizLinks);
        }
    }

    public void request(BizLinks bizLinks) {
        // 解决因jdk版本问题造成的SSL请求失败的问题
        java.lang.System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
        final HttpURLConnection connection;
        BizLinks byId = iBizLinksService.getById(bizLinks.getId());
        try {
            URL url = new URL(bizLinks.getUrl());
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            connection.setRequestProperty("Accept-Charset", "utf-8");
            log.info("RestClientUtil url: {}, response: {} : {}", bizLinks.getUrl(), connection.getResponseCode(), connection.getResponseMessage());
            Map<Integer, String> map = new HashMap<>(8);
            map.put(HttpsURLConnection.HTTP_INTERNAL_ERROR, "内部服务器错误");
            map.put(HttpsURLConnection.HTTP_BAD_REQUEST, "错误的请求");
            map.put(HttpsURLConnection.HTTP_GATEWAY_TIMEOUT, "网关超时");
            map.put(HttpsURLConnection.HTTP_UNAVAILABLE, "服务不可用");
            map.put(HttpsURLConnection.HTTP_BAD_GATEWAY, "错误的网关");
            map.put(HttpURLConnection.HTTP_NOT_FOUND, "找不到");
            map.put(HttpURLConnection.HTTP_CLIENT_TIMEOUT, "请求超时");
            map.put(HttpURLConnection.HTTP_MOVED_PERM, "永久移除");
            if (connection.getResponseCode() == HttpsURLConnection.HTTP_OK) {
                byId.setStatus(Boolean.TRUE);
            } else {
                byId.setStatus(Boolean.FALSE)
                        .setRemark(map.get(connection.getResponseCode()));
            }
            iBizLinksService.updateById(byId);
        } catch (Exception e) {
            log.error("Http请求失败{}", bizLinks.getUrl(), e);
            byId.setStatus(Boolean.FALSE)
                    .setRemark("不能访问");
            iBizLinksService.updateById(byId);
        }
    }
}
